package cn.xyfmzmy.adapter.spring.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * 使用适配器的思想，用etcd来替换redis
 * 表面使用redis的api，底层用etcd来实现
 */
@RestController
@RequestMapping("/redis")
public class KVController {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @GetMapping("/get/{key}")
    public String getValue(@PathVariable("key") String key){
        String value = redisTemplate.opsForValue().get(key);
        if(value == null){
            return "null";
        }
        return value;
    }

    @PostMapping("/put/{key}/{value}")
    public String putValue(@PathVariable("key") String key,@PathVariable("value") String value){
        redisTemplate.opsForValue().set(key,value);
        return "ok";
    }


}
